package com.feng.exception;

import com.feng.vo.ResultVo;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕获器
 * @author: 丰
 * @date: 2022/12/29 15:57
 */
@RestControllerAdvice
public class MyExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class);
    // 捕获所有运行时异常
    @ExceptionHandler(RuntimeException.class)
    public ResultVo<Object> exceptionHandler(RuntimeException e) {
        logger.error(e.getMessage(), e);
        ResultVo<Object> resultVo = ResultVo.systemError();
        String msg = resultVo.getMsg() + ": " + e.getMessage();
        return resultVo.message(msg);
    }
}
